#include "led.h"
#include "drv_gpio.h"

uint32_t blinkDelay[E_LED_MAX];
uint32_t blinkCount[E_LED_MAX];
uint8_t  blinkTimes[E_LED_MAX];
uint8_t  blinkFlag[E_LED_MAX];
uint8_t  blinkState;


void LED_BlinkSet(E_LED led,uint32_t delay, uint32_t times)
{
	blinkFlag[led]  = 1;
	blinkState |= (1<<led);
	blinkDelay[led] = delay;
	blinkCount[led] = delay;
	blinkTimes[led] = times*2;
	
}


void LED_Blink(uint8_t led)
{
	if(0 != (blinkState&(1<<led)))
	{
		if(blinkTimes[led] >0)
		{
			if(blinkCount[led]>0)
			{
				 blinkCount[led] --;		
			}
			else
			{
				blinkCount[led] = blinkDelay[led];
				if(0 == (blinkFlag[led]))
				{
					blinkTimes[led]--;
					if(E_LED_RED == led)
					{
						LED_RED_OFF();
					}
					else
					{
					  LED_GREEN_OFF();		
					}
					blinkFlag[led] = 1;
				}
				else
				{
					//blinkFlag &= ~(1<<led);
					blinkTimes[led]--;
					if(E_LED_RED == led)
					{
						LED_RED_ON();
					}
					else
					{
						LED_GREEN_ON();	
					}
					blinkFlag[led] = 0;
				}
			}
		}
		else
		{
			blinkState &= ~(1<<led);
		}
	}	
}


void LED_Process(void)
{
	LED_Blink(E_LED_RED);
	LED_Blink(E_LED_GREEN);
}

